package org.dairy.farms.nandana.core.logic;

import org.dairy.farms.dao.*;
import org.dairy.farms.model.*;
import org.dairy.farms.nandana.web.view.CheckupDetailsView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * User: gduggirala
 * Date: Aug 18, 2011
 * Time: 7:05:03 PM
 */
@Service("checkupManagementLogic")
public class CheckupManagementLogicImpl implements CheckupManagementLogic {
    private static final Logger logger = LoggerFactory.getLogger(CheckupManagementLogicImpl.class);

    @Autowired
    VaccinationManagementLogic vaccinationManagementLogic;
    @Autowired
    private CheckupDAO checkupDao;
    @Autowired
    private CheckupTypeDAO checkupTypeDao;
    @Autowired
    private ExpenseTypeDAO expenseTypeDao;
    @Autowired
    private PersonDAO personDao;
    @Autowired
    ExpenseDAO expenseDao;
    @Autowired
    private LivestockDAO livestockDao;
    @Autowired
    private MedicalAttentionDAO medicalAttentionDao;
    @Autowired
    private VaccineTypeDAO vaccineTypeDao;
    @Autowired
    private MedicalAttentionTypeDAO medicalAttentionTypeDao;
    @Autowired
    private VaccineDAO vaccineDao;
    @Autowired
    private CheckupNotesDAO checkupNotesDAO;
    @Autowired
    private MedicineDAO medicineDAO;
    @Autowired
    private MedicineTypeDAO medicineTypeDAO;

    public List<Checkup> getCheckupDetails(Long livestockId, int page, int start, int limit) {
        return checkupDao.getCheckupDetails(livestockId, page, start, limit);
    }

    public List<CheckupType> getAllCheckupTypes() {
        return checkupTypeDao.loadAllObjects();
    }

    public Checkup updateCheckupDetails(CheckupDetailsView checkupDetailsView) {
        Checkup checkup = null;
        CheckupType checkupType = checkupTypeDao.findById(checkupDetailsView.getCheckupTypeId());
        Person person = personDao.findById(checkupDetailsView.getDocId());
        ExpenseType expenseType = expenseTypeDao.findByName(ExpenseType.LIVESTOCK_CHECKUP_EXPENSE);
        Livestock livestock = livestockDao.findById(checkupDetailsView.getLivestockId());
        if (checkupDetailsView.getCheckupId() == null) {
            checkup = new Checkup();
            checkup.setDateOfCheckup(checkupDetailsView.getDateOfCheckup());
            checkup.setCheckupType(checkupType);
            checkup.setDoctor(person);
            checkup.setLivestock(livestock);
            //CheckupExpense checkupExpense = createCheckupExpense(checkupDetailsView, expenseType);

            //expenseDao.create(checkupExpense);
            //checkup.setCheckUpExpense(checkupExpense);
            checkupDao.create(checkup);
            createMedicalAttentionRecords(checkup, checkupDetailsView);
            createVaccineRecords(checkup, checkupDetailsView);
        } else {
            checkup = checkupDao.findById(checkupDetailsView.getCheckupId());
            checkup.setDateOfCheckup(checkupDetailsView.getDateOfCheckup());
            checkup.setCheckupType(checkupType);
            checkup.setDoctor(person);
            //CheckupExpense checkupExpense = createCheckupExpense(checkupDetailsView, expenseType);

            //expenseDao.update(checkupExpense);
            //checkup.setCheckUpExpense(checkupExpense);
            checkupDao.update(checkup);
        }

        return checkup;
    }

    private void createVaccineRecords(Checkup checkup, CheckupDetailsView checkupDetailsView) {
        if (checkupDetailsView.getVaccineTypes() != null) {
            for (String vTypeId : checkupDetailsView.getVaccineTypes()) {
                Long vaccineTypeId = -1L;
                vaccineTypeId = new Long(vTypeId);
                VaccineType vaccineType = vaccineTypeDao.findById(vaccineTypeId);
                Vaccine vaccine = new Vaccine();
                vaccine.setCheckup(checkup);
                vaccine.setVaccineType(vaccineType);
                vaccineDao.create(vaccine);
            }
        }
    }

    private void createMedicalAttentionRecords(Checkup checkup, CheckupDetailsView checkupDetailsView) {
        if (checkupDetailsView.getMedicalAttentionTypes() != null) {
            for (String dTypeId : checkupDetailsView.getMedicalAttentionTypes()) {
                Long diseaseTypeId = -1L;
                diseaseTypeId = new Long(dTypeId);
                MedicalAttentionType medicalAttentionType = medicalAttentionTypeDao.findById(diseaseTypeId);
                MedicalAttention medicalAttention = new MedicalAttention();
                medicalAttention.setMedicalAttentionType(medicalAttentionType);
                medicalAttention.setCheckup(checkup);
                medicalAttentionDao.create(medicalAttention);
            }

        }
    }

    private CheckupExpense createCheckupExpense(CheckupDetailsView checkupDetailsView, ExpenseType expenseType) {
        CheckupExpense checkupExpense = null;
        if (checkupDetailsView.getCheckupExpenseId() == null) {
            checkupExpense = new CheckupExpense();
            checkupExpense.setAmount(checkupDetailsView.getExpenseAmount());
            checkupExpense.setExpenseType(expenseType);
            checkupExpense.setPaymentDate(checkupDetailsView.getDateOfPayment());
        }else{
            checkupExpense = (CheckupExpense)expenseDao.findById(checkupDetailsView.getCheckupExpenseId());
            checkupExpense.setAmount(checkupDetailsView.getExpenseAmount());
            checkupExpense.setPaymentDate(checkupDetailsView.getDateOfPayment());
        }
        return checkupExpense;
    }

    @Override
    public List<MedicineType> getMedicineTypes(Long checkupTypeId) {
        List<MedicineType> medicineTypes = new ArrayList<MedicineType>();
        medicineTypes.addAll(checkupTypeDao.findById(checkupTypeId).getMedicineTypes());

        return medicineTypes;
    }

    public List<MedicalAttentionType> getAllDiseases() {
        return medicalAttentionTypeDao.loadAllObjects();
    }

    public List<VaccineType> getAllVaccineTypes() {
        return vaccinationManagementLogic.getAllVaccineTypes();
    }

    @Override
    public Notes addCheckupNotes(Long checkupId, String notes) {
        CheckupNotes checkupNotes = new CheckupNotes();
        checkupNotes.setNote(notes);
        checkupNotes.setCheckup(checkupDao.findById(checkupId));
        return checkupNotesDAO.create(checkupNotes);
    }
}
